package com.imooc.mall.controller;

import com.imooc.mall.common.ResultInfo;
import com.imooc.mall.dao.ShippingMapper;
import com.imooc.mall.dto.ShippingDto;
import com.imooc.mall.pojo.User;
import com.imooc.mall.service.IShippingService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import static com.imooc.mall.common.MallConst.CURRENT_USER;

@RestController
@RequestMapping("/shipping")
public class ShippingController {


    @Autowired
    private IShippingService shippingService;

    @PostMapping("/add")
    public ResultInfo add(HttpServletRequest request,
                          @RequestBody @Valid ShippingDto shippingDto){
        User user = (User) request.getSession().getAttribute(CURRENT_USER);
     return    shippingService.add(user.getId(),shippingDto);
    }
    @DeleteMapping("/delete/{shippingId}")
    public ResultInfo delete(HttpServletRequest request,
                             @PathVariable Integer shippingId){
        User user = (User) request.getSession().getAttribute(CURRENT_USER);
        return shippingService.delete(user.getId(),shippingId);
    }
    @PutMapping("/update/{shippingId}")
    public ResultInfo update(@PathVariable Integer shippingId,
                          @RequestBody @Valid ShippingDto shippingDto,
                          HttpServletRequest request){
        User user = (User) request.getSession().getAttribute(CURRENT_USER);
    return   shippingService.update(user.getId(),shippingId,shippingDto);
    }
    @RequestMapping("/list")
    public ResultInfo add(HttpServletRequest request,
                          @RequestParam(required = false,defaultValue = "1") Integer pageNum,
                          @RequestParam(required = false,defaultValue = "10")Integer pageSize){
        User user = (User) request.getSession().getAttribute(CURRENT_USER);
        return shippingService.list(user.getId(),pageNum,pageSize);
    }

}
